home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / ds.rexx < prev    next >
OS/2 REXX Batch file  |  2000-11-28  |  3KB  |  106 lines

  1. /*
  2. This is an example of a stand-alone/inedt service.
  3. It waits on port 10000/tcp for a pkt and send back
  4. a formatted date.
  5. It also opens an ARexx port called DS.10000 and waits
  6. for messages on it as well.
  7. The only command acccepted on the port is "QUIT".
  8. It can also be run as an inetd service, adding the service
  9. ds             10000/tcp
  10. to the services database, and the inetd entry
  11. dsc             stream tcp   nowait root   rxs rxs  path:ds.rexx
  12. to the inedt database.
  13.  
  14. ds accempt just on optiona argument:
  15. VERBOSE which makes ds to log the connections to syslog
  16.  
  17. dsfun.rexx is the single connection handler
  18. ds.rexx is the client for the service
  19. */
  20.  
  21. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then call err "rmh.library not found"
  22. if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then call err result "not found"
  23.  
  24. call initGlobal
  25.  
  26. ls=LastSocket()
  27. if ls>=0 then do
  28.     call handle(ls)
  29.     exit
  30. end
  31.  
  32. call createPort
  33. call createSocket
  34. call info "Started on port" global.port
  35. call start
  36. call info "Ended"
  37. exit
  38. /***************************************************************************/
  39. initGlobal: procedure expose global.
  40.     call pragma("D",PathPart(ProgramName("FULL")))
  41.     global.prg=ProgramName("NOEXT")
  42.     call SysLogCtl(global.prg)
  43.     if ~RMH_ReadArgs("VERBOSE/S") then call err DosString(IoErr())
  44.     global.verbose=parm.0.flag
  45.     call SetVar("VERBOSE",global.verbose,"LOCAL")
  46.     global.port=10000
  47.     return
  48. /***************************************************************************/
  49. err: procedure
  50. parse arg msg
  51.     call SysLog(msg,"ERR")
  52.     exit
  53. /***************************************************************************/
  54. info: procedure
  55. parse arg msg
  56.     call SysLog(msg,"INFO")
  57.     return
  58. /***************************************************************************/
  59. createPort: procedure expose global.
  60.     global.portName=upper(global.prg)"."global.port
  61.     if ~OpenPort(global.portName) then call err "can't create port"
  62.     global.ps=PortSignal(global.portName)
  63.     return
  64. /***************************************************************************/
  65. createSocket: procedure expose global.
  66.     global.sock=socket("INET","STREAM")
  67.     if global.sock<0 then call err "can't create socket %m"
  68.     local.addrFamily="INET"
  69.     local.addrPort=global.port
  70.     if bind(global.sock,"LOCAL")<0 then call err "can't bind socket %m"
  71.     if listen(global.sock,5)<0 then call err "can't listen on socket %m"
  72.     return
  73. /***************************************************************************/
  74. start: procedure expose global.
  75.     sel.read.0=global.sock
  76.     sigWait=or(2**12,global.ps)
  77.     open=1
  78.     do while open
  79.         res = WaitSelect("SEL",,,sigWait)
  80.         if and(sel.signals,2**12)~=0 then return
  81.         if res==1 then do
  82.             rsock=accept(global.sock,"REMOTE")
  83.             if rsock>=0 then call handle(rsock)
  84.         end
  85.         if and(sel.signals,global.ps)~=0 then do
  86.             pkt=GetPkt(global.PortName)
  87.             if pkt~=Null() then do
  88.                 comm=GetArg(pkt)
  89.                 res=0
  90.                 select
  91.                     when upper(comm)=="QUIT" then open = 0
  92.                     otherwise res=15
  93.                 end
  94.                 call Reply(pkt,res)
  95.             end
  96.         end
  97.     end
  98.     return
  99. /***************************************************************************/
  100. handle: procedure
  101. parse arg sock
  102.     call RXSCall("dsfun",sock)
  103.     call CloseSocket(sock)
  104.     return
  105. /***************************************************************************/
  106.